package com.huahai.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.huahai.config.Fallback;
import com.huahai.entity.Order;
import com.huahai.entity.Product;
import com.huahai.service.IProductService;
import com.huahai.service.impl.OrderService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试Sentinel
 */
@RestController
public class OrderController3 {

    private static int id = 1;

    @Autowired
    OrderService2 service2;

    @GetMapping("/order2/message1")
    public String message1() {
        ++id;
        if (id % 3 == 0) {
            throw new RuntimeException();
        }
        return "message1";
    }

    @GetMapping("/order2/message2")
    @SentinelResource(value = "testSentinelResource",
            fallbackClass = {Fallback.class},//指定出现异常后处理的类
            fallback = "fallback"//指定出现异常后处理的方法
    )
    public String message2() {
        return "message2";
    }

    @GetMapping("/order2/message3")
    @SentinelResource("message3")
    public String message3(@RequestParam(value = "name",required = false) String name,
                           @RequestParam(value = "age",required = false) Integer age) {
        return "热点规则:"+"name="+name+";age="+age;
    }

    /**
     * 测试授权规则
     * @return
     */
    @GetMapping("/order2/message4")
    public String message4(String serviceName) {
        return "message4";
    }

}
